package com.lyj.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lyj.model.Message;
import com.lyj.model.Option;
import com.lyj.model.Question;
import com.lyj.model.Questionnaire;
import com.lyj.model.Record;
import com.lyj.model.User;
import com.lyj.utils.DateUtil;

/**
 * 
 * @author 李艺江
 * @version 创建时间：2019年5月7日 上午12:04:03
 */

@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {

	private static final long serialVersionUID = 1L;

	public String send() {
		if (session.get("user") == null) {
			return "login";
		}
		request.put("questionnaires", questionnaireService.query());
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String pageName = (String) hsRequest.getParameter("pg");
		return pageName;
	}

	public String queryQuestuinnaires() {
		if (session.get("user") == null) {
			return "login";
		}
		User user = (User) session.get("user");
		List<Questionnaire> userQuestionnaires = questionnaireService.queryIsUsedQuestionnaires();
		for (Questionnaire questionnaire : userQuestionnaires) {
			for (Record record : questionnaire.getRecords()) {
				if (record.getUser().equals(user)) {
					questionnaire.setIsSurvey(1);
				}
			}
		}
		for (Questionnaire questionnaire : userQuestionnaires) {
			System.out.println(questionnaire.getIsSurvey());
		}
		request.put("questionnaires", userQuestionnaires);
		return "queryQuestionnaires";
	}

	public String questionnaireDetail() {
		if (session.get("user") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		return "questionnaireDetail";
	}

	public String doSurvey() {
		if (session.get("user") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid"));
		Questionnaire questionnaire = questionnaireService.get(qid);
		request.put("questionnaire", questionnaire);
		return "answerQuestions";
	}

	public String answerQuestions() {
		if (session.get("user") == null) {
			return "login";
		}
		User user = (User) session.get("user");
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qnid = Integer.parseInt((String) hsRequest.getParameter("qnid"));
		Questionnaire questionnaire = questionnaireService.get(qnid);
		int questionsCount = questionnaire.getQuestionsCount();
		for (int i = 1; i <= questionsCount; i++) {
			Integer qid = Integer.parseInt((String) hsRequest.getParameter("qid" + i));
			Question question = questionService.get(qid);
			Integer type = Integer.parseInt((String) hsRequest.getParameter("type" + i));
			if (type == 1) {
				String oidStr = hsRequest.getParameter("oid" + i);
				if (oidStr.equals("") || oidStr == null) {
					request.put("questionnaire", questionnaire);
					request.put("message", "每个题目都要作答!");
					return "answerQuestions";
				}
				Integer oid = Integer.parseInt(oidStr);
				Option option = optionService.get(oid);
				Record record = new Record(option, questionnaire, user, question);
				recordService.save(record);
			}
			if (type == 2) {
				String[] oidStrs = hsRequest.getParameterValues("oid" + i);
				if (oidStrs[0].equals("") && oidStrs[1].equals("") && oidStrs[2].equals("") && oidStrs[3].equals("")) {
					request.put("questionnaire", questionnaire);
					request.put("message", "每个题目都要作答!");
					return "answerQuestions";
				}
				for (String oidStr : oidStrs) {
					Integer oid = Integer.parseInt(oidStr);
					Option option = optionService.get(oid);
					Record record = new Record(option, questionnaire, user, question);
					recordService.save(record);
				}
			}
		}		
		request.put("message", "答题成功!");		
		request.put("questionnaire", questionnaire);
		return "questionnaireDetail";
	}
	
	public String leaveMessage() throws ParseException {
		if (session.get("user") == null) {
			return "login";
		}
		User user = (User) session.get("user");
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String title = hsRequest.getParameter("title");
		String content = hsRequest.getParameter("content");
		Long time = DateUtil.nowToLong();
		Message message = new Message(user, title, content, time);
		messageService.save(message);
		request.put("message", message);
		request.put("tip", "添加成功!");
		return "lm";
	}
	
	public String changePwd() {
		if (session.get("user") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		String oldPwd = hsRequest.getParameter("oldPwd");
		String newPwd = hsRequest.getParameter("newPwd");
		String newPwd1 = hsRequest.getParameter("newPwd1");
		request.put("oldPwd", oldPwd);
		request.put("newPwd", newPwd);
		request.put("newPwd1", newPwd1);
		if (oldPwd.equals("")) {
			request.put("oldPwdErr", "密码不能为空!");
			return "cp";
		}
		if (newPwd.equals("")) {
			request.put("newPwdErr", "密码不能为空!");
			return "cp";
		}
		if (newPwd1.equals("")) {
			request.put("new1PwdErr", "密码不能为空!");
			return "cp";
		}
		if (!newPwd.equals(newPwd1)) {
			request.put("newPwdErr", "新密码不一致!");
			return "cp";
		} else {
			User user = (User) session.get("user");
			if (!user.getPassword().equals(oldPwd)) {
				request.put("oldPwdErr", "原密码不正确!");
				return "cp";
			} else {
				user.setPassword(newPwd);
				userService.update(user);
				request.put("message", "更改密码成功!");
				session.put("user", user);
				return "cp";
			}
		}
	}
	
	public String manageAll() {
		if (session.get("user") == null) {
			return "login";
		}
		User user = (User) session.get("user");
		Integer uid = user.getId();
		user = userService.get(uid);
		List<Questionnaire> questionnaires = new ArrayList<Questionnaire>();
		for(Record record : user.getRecords()) {
			questionnaires.add(record.getQuestionnaire());
		}
		HashSet<Questionnaire> hashSet = new HashSet<Questionnaire>(questionnaires);
		questionnaires.clear();
		questionnaires.addAll(hashSet);
		request.put("questionnaires", questionnaires);
		request.put("messages", user.getMessages());
		return "ui";
	}
	
	public String deleteQuestionnaire() {
		if (session.get("user") == null) {
			return "login";
		}
		User user = (User) session.get("user");
		Integer uid = user.getId();
		user = userService.get(uid);
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer qid = Integer.parseInt((String)hsRequest.getParameter("qid"));
		for(Record record : user.getRecords()) {
			if(record.getQuestionnaire().getId().equals(qid)) {
				recordService.delete(record.getId());
			}
		}
		request.put("tip", "删除成功!");
		return manageAll();
	}
	
	public String deleteMessage() {
		if (session.get("user") == null) {
			return "login";
		}
		HttpServletRequest hsRequest = ServletActionContext.getRequest();
		Integer mid = Integer.parseInt((String)hsRequest.getParameter("mid"));
		messageService.delete(mid);
		request.put("tip", "删除成功!");
		return manageAll();
	}
}
